# -*- coding: utf-8 -*-

import jieba
import jieba.posseg
from resume.apps import *
from common.ReadFile import *
from common.WriteFile import *


class Participle(object):
    """
    在这里对结巴分词进行二次封装
    把一些常用的初始化放在__init__函数中
    避免将相同的初始化动作分散在代码中
    同时提供一些快捷调用结巴方法的入口
    """
    DEFAULT_DICTIONARY_PATH = ResumeConfig.path + '/data/participle_dict.txt'
    jb = jieba
    seg = jieba.posseg

    # 处理词性
    property_path = ResumeConfig.weight_path + '/property'
    _property = read(property_path, READ_LINE_LIST, ' ', 2, False)

    def __init__(self, path=DEFAULT_DICTIONARY_PATH):
        """
        默认加载自定义的词典
        在此处加载自定义字典会对posseg同时起作用
        :param path:
        """
        self.jb.load_userdict(path)
        pass

    @classmethod
    def seg_cut(cls, sentence, hmm=True):
        words = seg.cut(sentence, hmm)
        w = []
        f = []
        for word, flag in words:
            # 去除空格
            if word.strip() == '':
                continue
                pass
            w.append(word)
            f.append(flag)
            if flag not in cls._property:
                cls._property.append(flag)
                # 触发写入词性
                write_data(cls._property, cls.property_path)
                pass
            pass
        rs = {
            'word': w,
            'flag': f
        }
        return rs
        pass

    pass


participle = Participle()
jb = participle.jb
seg = participle.seg
cut = jb.cut
seg_cut = participle.seg_cut
